package com.example.shop.fgo.ui.activity;

import android.app.ProgressDialog;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.example.shop.fgo.Constant;
import com.example.shop.fgo.R;
import com.example.shop.fgo.base.YBaseActivity;
import com.example.shop.fgo.entity.FileListEntity;
import com.example.shop.fgo.net.FileListServe;
import com.example.shop.fgo.ui.adapter.ItemVPDT;
import com.example.shop.fgo.utils.CookieUtil;
import com.example.shop.fgo.utils.TimeUtil;
import com.example.shop.library.net.NetJsonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DTActivity extends YBaseActivity implements Callback<FileListEntity> {

    FileListEntity.DataEntity mDataEntityNow;//当前显示谁
    private List<FileListEntity.DataEntity> mDataEntitiesAll;//其他显示谁

    private ViewPager mVpDtActivity;
    private ItemVPDT mItemVPDT;
    public TextView mTvTimeDt;
    public TextView mTvPosDt;
    public  int totalCount;
    ProgressDialog mProgressDialog;

    @Override
    public int setRootView() {
        return R.layout.activity_dt;
    }

    @Override
    public void initViews() {
        mProgressDialog=new ProgressDialog(this);
        mProgressDialog.setTitle("请稍等");
        mProgressDialog.setCancelable(false);
        mProgressDialog.setMessage("正在努力获取数据，请稍等....");
        mProgressDialog.show();
        mTvTimeDt = (TextView) findViewById(R.id.tv_time_dt);
        mTvPosDt = (TextView) findViewById(R.id.tv_pos_dt);
        mVpDtActivity = (ViewPager) findViewById(R.id.vp_dt_activity);
        mVpDtActivity.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                mDataEntityNow=mDataEntitiesAll.get(position);
                mTvTimeDt.setText(TimeUtil.formatTimeInS(mDataEntityNow.getT()));
                mTvPosDt.setText(position+1+"/"+totalCount);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initDatas() {

        //点击单个实体，跳转到查看大图Activity界面  putExtra携带了点击图片实体类的数据
        mDataEntityNow= (FileListEntity.DataEntity) this.getIntent().getSerializableExtra("data");

        getAllDatas();
    }

    public void getAllDatas() {
        Map<String, Object> mMapRefreshParams = new HashMap<>();
        mMapRefreshParams.put("ct", "list");
        mMapRefreshParams.put("aid", "1");
        mMapRefreshParams.put("o", Constant.SORT_USER_PTIME);
        mMapRefreshParams.put("asc", "0");
        mMapRefreshParams.put("offset", "0");
        mMapRefreshParams.put("limit",70);
        mMapRefreshParams.put("cid", "0");
        mMapRefreshParams.put("type", "2");

        FileListServe fileListServe = NetJsonUtil.getRetrofit(Constant.BASE_URL, FileListServe.class);
        Call<FileListEntity> call = fileListServe.requestFiles(mMapRefreshParams, CookieUtil.getCookie());
        call.enqueue(this);
    }

    @Override
    public void onResponse(Call<FileListEntity> call, Response<FileListEntity> response) {
        FileListEntity fileListEntity = response.body();

        mDataEntitiesAll = fileListEntity.getData();
        System.out.println(mDataEntitiesAll+"");
        totalCount=fileListEntity.getCount()-1;

        mItemVPDT = new ItemVPDT(this,mDataEntitiesAll);
        mVpDtActivity.setAdapter(mItemVPDT);

        int posStart=0;
        for (int i = 0; i < mDataEntitiesAll.size(); i++) {
            FileListEntity.DataEntity dataEntity=mDataEntitiesAll.get(i);
            if (dataEntity.getFid().equals(mDataEntityNow.getFid())) {
                posStart=i;
                break;
            }
        }
        mVpDtActivity.setCurrentItem(posStart);
        mTvTimeDt.setText(TimeUtil.formatTimeInS(mDataEntityNow.getT()));
        mTvPosDt.setText(posStart+1+"/"+totalCount);
        mProgressDialog.cancel();
    }

    @Override
    public void onFailure(Call<FileListEntity> call, Throwable t) {
        mProgressDialog.cancel();
    }

    @Override
    public boolean isNeedTitle() {
        return false;
    }
}
